'use strict';

var moment = require('moment');

module.exports = app => {
  const {
    INTEGER,
    STRING,
    DATE,
  } = app.Sequelize;

  const Topic = app.model.define('topics', {
    tid: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    uid: INTEGER,
    cityType: STRING,
    departureTime: {
      type: DATE,
      get() {
        return moment(this.getDataValue('departureTime')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    peopleNum: INTEGER,
    topicType: STRING,
    remark: STRING,
    linkman: STRING,
    linkPhone: STRING,
    state: INTEGER,
    browseCount: {
      type: INTEGER,
      defaultValue: 0
    },
    
    created_at: {
      type: DATE,
      get() {
        return moment(this.getDataValue('created_at')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    updated_at: {
      type: DATE,
      get() {
        return moment(this.getDataValue('updated_at')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
  });

  Topic.associate = function() {
    app.model.Topic.hasMany(app.model.Comment,{
      foreignKey: 'tid',
    });
    app.model.Topic.belongsTo(app.model.Config,{
      targetKey: 'code',
      foreignKey: 'cityType',
      as: 'cityConfig'
    });
  };

  return Topic;
};